package com.noassertions.utils;

import java.io.File;

/**
 * Set of utils to aid in file creation, management, and deletion
 * 
 * @author Matthew Phillips, mphillip@vt.edu, 20091209
 */
public class FileUtil {

	/**
	 * This method checks to see if the target filename already exists, if it
	 * doesn't great, use that file name. If it it does, create a filename with
	 * a number prepended.
	 * 
	 * Accepts a desired file name (without the path, just the filename) and
	 * returns a File object
	 * @author Matthew Phillips, mphillip@vt.edu, 20091209
	 */
	public static synchronized File findStorageLocaiton(String originalName, String imageStorePath) {

		// TODO build date stamp file hierarchy from imageStorePath
		File destinationDir = new File(imageStorePath);
		if (!destinationDir.exists()) {
			destinationDir.mkdirs();
		}
		
		File file = new File(imageStorePath + "/" + originalName);
		
		// If our filename exists, prepend filename with a number
		int count = 0;
		while (file.exists()) {
			file = new File(imageStorePath + "/" + count + originalName);
			count++;
		}
				
		return file;
	}
}
